package com.pjnkj.thunder.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pjnkj.thunder.common.json.Body;
import com.pjnkj.thunder.common.json.Meta;
import com.pjnkj.thunder.common.utils.MapUtils;
import com.pjnkj.thunder.entity.Shop;
import com.pjnkj.thunder.entity.Task;
import com.pjnkj.thunder.service.ShopService;
import com.pjnkj.thunder.service.TaskService;
import org.gavaghan.geodesy.Ellipsoid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import static com.pjnkj.thunder.common.json.Meta.CODE_470;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author pjnkj
 * @since 2022-01-20
 */
@RestController
@RequestMapping("/thunder/task")
public class TaskController {
    @Autowired
    TaskService taskService;

    @Autowired
    ShopService shopService;

    /**
     * 删除数据
     *
     * @param task
     */
    @PostMapping("delete")
    public Body delete(Integer id) {
        Boolean flag = taskService.removeById(id);
        if (flag == true) {
            return Body.newInstance("删除成功");
        }
        return Body.newInstance(Meta.CODE_400, "删除失败");
    }

    /**
     * 增加数据
     *
     * @param task
     */
    @PostMapping("create")
    public Body create(@Validated(Task.class) @RequestBody Task task) {
        Boolean flag = taskService.saveOrUpdate(task);
        if (flag == true) {
            return Body.newInstance("保存成功");
        }
        return Body.newInstance(Meta.CODE_400, "保存失败");
    }

    /**
     * 修改数据
     *
     * @param task
     */
    @PostMapping("update")
    public Body update(@Validated(Task.class) @RequestBody Task task) {
        Boolean flag = taskService.updateById(task);
        if (flag == true) {
            return Body.newInstance("修改成功");
        }
        return Body.newInstance(Meta.CODE_400, "修改失败");
    }

    /**
     * 查询数据
     *
     * @param
     */
    @PostMapping("find")
    public Body find(String id) {
        Task info = taskService.getById(id);
        if (info != null) {
            return Body.newInstance(info);
        }
        return Body.newInstance(CODE_470, "查询失败");
    }


    /**
     * 查询数据list
     *
     * @param
     */
    @PostMapping("findlist")
    public Body findlist() {
        List<Task> list = taskService.list();
        if (list != null && list.size() > 0) {
            return Body.newInstance(list);
        }
        return Body.newInstance(CODE_470, "查询失败");
    }

    /**
     * 分页查询数据
     *
     * @param
     */
    @PostMapping("page")
    public Body page(Integer current, Integer size, String longitude, String latitude) {
        Page<Task> pg = new Page<Task>(current, size);
        IPage<Task> list = taskService.page(pg);

        if (list != null && list.getSize() > 0) {
            List<Task> records = list.getRecords();
            for (int i = 0; i < records.size(); i++) {
                Task task = records.get(i);
                Shop byId = shopService.getById(task.getShopid());
                if (byId != null) {
                    records.get(i).setShop(byId);
                }

            }
            java.text.DecimalFormat df = new java.text.DecimalFormat("#0.00");

            records = records.stream().sorted(Comparator.comparing((Task t) ->
                    {
                        double v = MapUtils.getDistanceMeter(t.getShop().getLatitude(), t.getShop().getLongitude(), latitude, longitude, Ellipsoid.Sphere);
                        t.setDistance(String.valueOf(df.format(v / 1000)));
                        return v;
                    }))
                    .filter((Task t) -> Double.valueOf(t.getDistance()) <= 5).collect(Collectors.toList());// 根据距离逆序
            list.setRecords(records);
            return Body.newInstance(list);
        }
        return Body.newInstance(CODE_470, "查询失败");
    }
}
